package com.clt.ess.service.impl;

import com.clt.ess.dao.IPersonDao;
import com.clt.ess.dao.IUserDao;
import com.clt.ess.entity.Person;
import com.clt.ess.entity.Seal;
import com.clt.ess.entity.Unit;
import com.clt.ess.entity.User;
import com.clt.ess.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.clt.ess.utils.StringUtils.getDecryptString;


@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    IUserDao userDao = null;
    @Autowired
    IPersonDao personDao = null;
    @Override
    public User findUser(User user) {
        return DecryptUser(userDao.findUserById(user.getUserId()));
    }

    @Override
    public User findUserById(String id) {
        return DecryptUser(userDao.findUserById(id));
    }
    @Override
    public List<User> findUserList(User user) {
        List<User> userList =userDao.findUserList(user);
        for (User user1:userList){
            DecryptUser(user1);
        }
        return userDao.findUserList(user);
    }


    @Override
    public List<User> findLoginUserByPersonId(User user) {
        List<User> userList =userDao.findLoginUserByPersonId(user);
        for (User user1:userList){
            DecryptUser(user1);
        }
        return userList;
    }

    /**
     * 根据关键字查找人员列表
     * @param keyword 关键词
     */
    @Override
    public List<Person> findPersonListByKeyword(String keyword) {
        return personDao.findPersonListByKeyword(keyword);
    }


    private User DecryptUser(User user){
        if (user==null){
            return null;
        }
        if (user.getPerson()!=null){
            if (user.getPerson().getPersonName()!=null){
                user.getPerson().setPersonName(getDecryptString(user.getPerson().getPersonName()));
            }
            if (user.getPerson().getPhone()!=null){
                user.getPerson().setPhone(getDecryptString(user.getPerson().getPhone()));
            }
            if (user.getPerson().getIdNum()!=null){
                user.getPerson().setIdNum(getDecryptString(user.getPerson().getIdNum()));
            }
            return user;
        }
        if (user.getUnit()!=null){
            if (user.getUnit().getUnitName()!=null){
                user.getUnit().setUnitName(getDecryptString(user.getUnit().getUnitName()));
            }
            if (user.getUnit().getCredit_code()!=null){
                user.getUnit().setCredit_code(getDecryptString(user.getUnit().getCredit_code()));
            }
            if (user.getUnit().getArea_number()!=null){
                user.getUnit().setArea_number(getDecryptString(user.getUnit().getArea_number()));
            }
        }
        return user;
    }

}
